package com.ysu.yybk.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间管理类
 * @author ：lhw
 * @date ：Created in 2019/11/18 22:23
 */
public class TimeUtil {
    //获取第几周
    public static Integer getWeek(){
        Long week=(System.currentTimeMillis()-1582473600000L)/604800000L+1L;

        return week.intValue();
    }

    //获取第几节课
    public static Integer getTime() {
        Integer ret = -1;
        String format = "HH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            if(isEffectiveDate(new SimpleDateFormat(format).parse("07:50:00"),new SimpleDateFormat(format).parse("09:45:00")))
                ret=1;
            else if(isEffectiveDate(new SimpleDateFormat(format).parse("09:45:00"),new SimpleDateFormat(format).parse("11:50:00")))
                ret=3;
            else if(isEffectiveDate(new SimpleDateFormat(format).parse("13:20:00"),new SimpleDateFormat(format).parse("15:15:00")))
                ret=5;
            else if(isEffectiveDate(new SimpleDateFormat(format).parse("15:25:00"),new SimpleDateFormat(format).parse("17:20:00")))
                ret=7;
            else if(isEffectiveDate(new SimpleDateFormat(format).parse("18:00:00"),new SimpleDateFormat(format).parse("19:55:00")))
                ret=9;
            else if(isEffectiveDate(new SimpleDateFormat(format).parse("19:55:01"),new SimpleDateFormat(format).parse("21:50:00")))
                ret=11;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ret;
    }

    //时间比对
    public static boolean isEffectiveDate(Date startTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        String format = "HH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(format);
        String time=df.format(new Date());
        try {
            date.setTime(new SimpleDateFormat(format).parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

        if(date.getTime().equals(startTime) || date.getTime().equals(endTime)){
            return true;
        }

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);


        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    //获取周几
    public static Integer getDate(){
        Calendar cal = Calendar.getInstance();
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(w <= 0)
            w = 7;
        return w;
    }

}